package com.hui.forecastapp.city_manager;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hui.forecastapp.R;
import com.hui.forecastapp.bean.WeatherBean;
import com.hui.forecastapp.db.DatabaseBean;

import java.util.List;

public class CityManagerAdapter extends BaseAdapter {
    Context context;
    List<DatabaseBean> mDatas;

    public CityManagerAdapter(Context context, List<DatabaseBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_city_manager, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        DatabaseBean bean = mDatas.get(position);
        holder.cityTv.setText(bean.getCity());
        WeatherBean weatherBean = new Gson().fromJson(bean.getContent(), WeatherBean.class);
        // 获取今日天气情况
        WeatherBean.DataDTO.ObserveDTO todayBean = weatherBean.getData().getObserve();
        // 设置TextView
        holder.currentTempTv.setText(todayBean.getDegree() + "℃");
        holder.windTv.setText("风力 " + todayBean.getWind_power());
        holder.humitidyTv.setText("湿度 " + todayBean.getHumidity() + "%");
        holder.conTv.setText(todayBean.getWeather());

        return convertView;
    }

    class ViewHolder {
        TextView cityTv, conTv, currentTempTv, windTv, humitidyTv;

        public ViewHolder(View view) {
            cityTv = view.findViewById(R.id.item_city_tv_city);
            conTv = view.findViewById(R.id.item_city_tv_condition);
            currentTempTv = view.findViewById(R.id.item_city_tv_temp);
            windTv = view.findViewById(R.id.item_city_tv_wind);
            humitidyTv = view.findViewById(R.id.item_city_tv_humidity);
        }
    }
}
